package com.timmy.video.ui.activity;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.timmy.video.R;
import com.timmy.video.ui.fragment.AlertDialogFragment;

/**
 * @Auther: Timmy
 * @Data: 2017/7/22
 * @Fuction:
 */

public class DialogActivity extends AppCompatActivity implements View.OnClickListener, AlertDialogFragment.DialogFragmentDataImp {


    private static final String TAG = DialogActivity.class.getSimpleName();
    private Button mBtnShowDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dialog);

        mBtnShowDialog = (Button) findViewById(R.id.dialog_btn);

        mBtnShowDialog.setOnClickListener(this);

        testOkgo();
    }

    private void testOkgo() {

        Log.i(TAG, "-----------------------: 开始进行网络请求");
        OkGo.<String>get("http://gank.io/api/data/Android/20/1")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        Log.i(TAG, "-----------------------:  请求成功");

                        String body = response.body();


                        Log.i(TAG, "-----------------------:  请求成功 " + body);

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);


                        Log.i(TAG, "-----------------------:  请求失败");
                    }
                });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.dialog_btn:
                // showDialogFragment();

                // new Dialog(this).show(getSupportFragmentManager(), "bag");

                startActivity(new Intent(this, DialogActivity2.class));
                break;

        }
    }

    public void showDialogFragment() {
        FragmentTransaction mFragTransaction = getFragmentManager().beginTransaction();
        Fragment fragment = getFragmentManager().findFragmentByTag("dialogFragment");
        if (fragment != null) {
            //为了不重复显示dialog，在显示对话框之前移除正在显示的对话框
            mFragTransaction.remove(fragment);
        }
        AlertDialogFragment dialogFragment = AlertDialogFragment.newInstance("are you ok?");
        dialogFragment.show(mFragTransaction, "dialogFragment");//显示一个Fragment并且给该Fragment添加一个Tag，可通过findFragmentByTag找到该Fragment
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
